package com.softline.documentsmall.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softline.documentsmall.entity.Dictionary;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author wph
 * @Date 2021/2/26 15:58
 */
@Api(value = "后台管理___语音相关接口", tags = {"后台管理___语音相关接口"})
@RestController
@RequestMapping("/dictionaryLocal")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class DictionaryLocalController {
    @Autowired
    DictionaryService dictionaryService;

    @ApiOperation(value = "后台管理___查询语音列表", notes = "后台管理___查询语音列表")
    @GetMapping(value = "/list")
    public ReturnVO list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,@RequestParam(name = "id", required = false) Integer id, @RequestParam(name = "title", required = false) String title) {
        JSONObject resultObject = new JSONObject();
        Page<Dictionary> result = new Page<>();
        List<Map<String, Object>> records = dictionaryService.selectList(id,title,pageNo, pageSize);
        Long count = dictionaryService.selectLocalCount(id,title);
        resultObject.put("records",records);
        resultObject.put("total",count);
        resultObject.put("size",pageSize);
        resultObject.put("current",pageNo);
        resultObject.put("searchCount",true);
        long pages = count / pageSize;
        if (count % pageSize != 0) {
            pages++;
        }
        resultObject.put("pages",pages);
        return ReturnVOUtils.success(resultObject);
    }

    @ApiOperation(value = "后台管理___查询语音详情", notes = "后台管理___查询语音详情")
    @GetMapping(value = "/selectById")
    public JSONObject selectById(@RequestParam(name = "id") int id) {
        return dictionaryService.selectById(id);
    }

    @ApiOperation(value = "后台管理___新增语音", notes = "后台管理___新增语音")
    @PostMapping(value = "/insert")
    public ReturnVO insert(@RequestBody Dictionary dictionary) {
        return ReturnVOUtils.success(dictionaryService.insert(dictionary));
    }

    @ApiOperation(value = "后台管理___修改语音", notes = "后台管理___修改语音")
    @PostMapping(value = "/update")
    public ReturnVO update(@RequestBody Dictionary dictionary) {
        return ReturnVOUtils.success(dictionaryService.update(dictionary));
    }

}
